package com.miguo.db;

import java.util.List;

import de.greenrobot.dao.query.QueryBuilder;

import com.miguo.app.AppContext;
import com.miguo.bean.AddressBean;
import com.miguo.db.AddressDao.Properties;

import android.content.Context;

public class AddressDB {

	private static Context mContext;

	private static AddressDB instance;

	private AddressDao addressDao;

	private AddressDB() {
	}

	public static AddressDB getInstance(Context context) {
		if (instance == null) {
			instance = new AddressDB();
			if (mContext == null) {
				mContext = context;
			}

			// 数据库对象
			DaoSession daoSession = AppContext.getDaoSession(mContext, false);
			instance.addressDao = daoSession.getAddressDao();
		}
		return instance;
	}

	public long save(AddressBean goods) {
		return addressDao.insertOrReplace(goods);
	}

	public void saveList(final List<AddressBean> list) {
		if (list == null || list.isEmpty()) {
			return;
		}
		addressDao.getSession().runInTx(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < list.size(); i++) {
					AddressBean note = list.get(i);
					addressDao.insertOrReplace(note);
				}
			}
		});

	}

	public void deleteAll() {
		addressDao.deleteAll();
	}

	public List<AddressBean> Parentid(Long parentid) {
		QueryBuilder<AddressBean> address = addressDao.queryBuilder();
		address.where(Properties.Parentid.eq(parentid));
		return address.list();
	}

	public boolean isAddress() {
		long size = addressDao.count();
		if (size > 0) {
			return true;
		}
		return false;
	}

}